<?php

/**
 *    合作伙伴控制器
 *
 *    @author    Garbin
 *    @usage    none
 */
class ReturnsApp extends BackendApp
{
    /**
     *    退货退款控制器
     *
     *    @author    yanghj
     *    @date    2014.12.02
     */
    function index()
    {
        $search_options = array(
            'seller_name'   => Lang::get('store_name'),
            'buyer_name'   => Lang::get('buyer_name'),
            'order_sn'   => Lang::get('order_sn'),
            'returns_sn'   => Lang::get('returns_sn'),
            'out_trade_sn'   => Lang::get('out_trade_sn'),
        );
        $cfg_returns = $this->_get_cfg_msg();
        $data = $this->_get_returns();

        $this->assign('filtered', $data['filtered']); //是否有查询条件
        $this->assign('returns_status_list', $cfg_returns['starus_type']);
        $this->assign('search_options', $search_options);
        $this->assign('page_info', $data['page']);          //将分页信息传递给视图，用于形成分页条
        $this->assign('returns_list', $data['data']);
        $this->import_resource(array('script' => 'inline_edit.js,jquery.ui/jquery.ui.js,jquery.ui/i18n/' . i18n_code() . '.js',
                                      'style'=> 'jquery.ui/themes/ui-lightness/jquery.ui.css'));
        $this->display('returns.index.html');
    }

    /**
     *    查看
     *
     *    @author    Garbin
     *    @param    none
     *    @return    void
     */
    function view()
    {
        $returns_id = isset($_GET['id']) ? intval($_GET['id']) : 0;
        if (!$returns_id)
        {
            $this->show_warning('no_such_returns');

            return;
        }

        $cfg_returns = $this->_get_cfg_msg();
        $model_returns =& m('returns');
        $model_returns_log =& m('returnslog');
        $returns_info = $model_returns->get(array(
            'conditions'    => $returns_id,
            'fields'        => "this.*,order_goods.rec_id,order_goods.goods_id,order_goods.goods_image,order_goods.quantity as goods_quantity,order_goods.goods_name,order_goods.specification,order_alias.order_sn,order_alias.out_trade_sn,order_alias.seller_name",
            'join'          => 'belongs_to_order,belongs_to_ordergoods',
        ));

        $returns_log = $model_returns_log->find(array(
            'conditions'    => "returns_id={$returns_id} ",
            'order'         => 'log_time DESC',
        ));

        if (!$returns_info)
        {
            $this->show_warning('no_such_returns');
            return;
        }
        $returns_info['returns_status'] = $cfg_returns['status'][$returns_info['status']][$returns_info['type']]['msg_2'];//退货退款状态提示

        $this->assign('returns', $returns_info);
        $this->assign('returns_type_arr', $cfg_returns['type']);
        $this->assign('returns_log',$returns_log);
        $this->display('returns.view.html');

        
    }
    
    function add_track()
    {
        $returns_id = isset($_GET['id']) ? $_GET['id'] : 0;
        $track_type = 2;
        if(!$returns_id){
            $this->show_warning('no_such_returns');
            return;
        }
        
        $track_mod = m("infotrack");
        
        if(!IS_POST){
            $tracks = $track_mod->find("track_id='{$returns_id}' AND track_type='{$track_type}'");
            $this->assign("tracks", $tracks);
            $this->assign("title_name", '退货跟进');
            $this->import_resource('jquery.plugins/jquery.validate.js');
            $this->display("order.track.html");
        }else{
            $track_user = html_script(trim($_POST['track_user']));
            $track_info = html_script(trim($_POST['track_info']));
            
            if(!$track_info || !$track_user){
                $this->show_warning('表单信息不全');
                return;
            }
            
            $track_mod->add(array(
                    'track_id' => $returns_id,
                    'track_type' => $track_type,
                    'track_user' => $track_user,
                    'track_info' => $track_info,
                    'track_time' => time(),
            ));
            
            $this->show_message('添加退货跟进信息成功！');
            return;
        }
    }

    /**
     *    添加退货运费
     *
     */
    function add_shipping_fee()
    {
        $returns_id = isset($_GET['id']) ? intval($_GET['id']) : 0;
        if (!$returns_id)
        {
            $this->show_warning('no_such_returns');

            return;
        }

        $model_returns =& m('returns');
        $model_shipping =& m('returnsshippingfee');

        $returns_info = $model_returns->get(array(
            'conditions'    => $returns_id,
            'fields'        => "this.returns_id,this.returns_sn,this.returns_sn,order_alias.order_sn",
            'join'          => 'belongs_to_order',
        ));

        if (!$returns_info)
        {
            $this->show_warning('no_such_returns');
            return;
        }

        if (!IS_POST)
        {

            $this->import_resource(array(
                'script' => 'jquery.plugins/jquery.validate.js'
            ));

            $this->assign('payment_type', array('1'   => '网银','2'   => '支付宝','3'=>'财付通','4'=>'翼支付'));
            $this->assign('returns', $returns_info);
            $this->display('returns_shipping_fee.form.html');
        }
        else
        {
            $buyer_shipping_fee = floatval($_POST['buyer_shipping_fee']);
            if($buyer_shipping_fee<=0 || empty($_POST['real_name']) || empty($_POST['bank_card']) || empty($_POST['phone']))
            {
                $this->show_warning('填写的数据不正确,请重新填写!');
                return;
            }
            if(intval($_POST['payment_type']) == 1 && empty($_POST['bank_address']))
            {
                $this->show_warning('开户行不能为空!');
                return;
            }

            $data = array();
            $data['returns_id']     = $returns_info['returns_id'];
            $data['order_sn']     = $returns_info['order_sn'];
            $data['real_name']     = html_script($_POST['real_name']);
            $data['bank_card']     = html_script($_POST['bank_card']);
            $data['payment_type']  = intval($_POST['payment_type']);
            $data['bank_address']  = html_script($_POST['bank_address']);
            $data['identity_card']  = html_script($_POST['identity_card']);
            $data['phone']  = html_script($_POST['phone']);
            $data['add_time'] = gmtime();


            if (!$model_shipping->add($data))
            {
                $this->show_warning($model_shipping->get_error());

                return;
            }

            $model_returns->edit($returns_id, array('buyer_shipping_fee' => $buyer_shipping_fee,'shipping_status'=>1));
            if ($model_returns->has_error())
            {
                $this->show_warning($model_returns->get_error());

                return;
            }

            $this->admin_log('添加退货运费:退货编号为'.$returns_info['returns_sn']);
            $this->show_message('添加退货运费成功',
            'back_list',        'index.php?app=returns'
            );
        }
    }

    /**
     *    查看
     *
     *    @author    Garbin
     *    @param    none
     *    @return    void
     */
    function view_shipping_fee()
    {
        $returns_id = isset($_GET['id']) ? intval($_GET['id']) : 0;
        if (!$returns_id)
        {
            $this->show_warning('no_such_returns');

            return;
        }

        $model_shipping =& m('returnsshippingfee');
        $returns_info = $model_shipping->get(array(
            'conditions'    => "returns_shipping_fee.returns_id = ".$returns_id,
            'fields'        => "this.*,returns.returns_sn,returns.buyer_shipping_fee,returns.shipping_status",
            'join'          => 'belongs_to_returns',
        ));


        if (!$returns_info)
        {
            $this->show_warning('no_such_returns');
            return;
        }

        $this->assign('payment_type', array('1'   => '网银','2'   => '支付宝','3'=>'财付通','4'=>'翼支付'));
        $this->assign('returns', $returns_info);
        $this->display('returns_shipping_fee.view.html');


    }



    /**
     *    导出数据
     */
    function export()
    {

        $data = $this->_get_returns(true);
        $export_data = $data['data'];

        if($export_data)
        {
            if(count($export_data) > 10000){
                echo "<script>alert('数据量不能大于10000，请筛选查询分批导出');history.back();</script>";
                exit;
            }
            else
            {
                $heads = $this->_get_excel_heads();
                require(ROOT_PATH."/includes/PHPExcel/export_excel.php");
                createExcel('退货退款', $heads, $export_data);
                exit;
            }
        }
        else
        {
            echo "<script>alert('没有相关的数据');history.back();</script>";
            exit;
        }
    }

    /**
     *    获取退货退款列表
     */
    function _get_returns($is_excel = false)
    {
        $arr = array();
        $cfg_returns = $this->_get_cfg_msg();
        $search_options = array(
            'seller_name'   => Lang::get('store_name'),
            'buyer_name'   => Lang::get('buyer_name'),
            'order_sn'   => Lang::get('order_sn'),
            'returns_sn'   => Lang::get('returns_sn'),
            'out_trade_sn'   => Lang::get('out_trade_sn'),
        );
        /* 默认搜索的字段是店铺名 */
        $field = 'seller_name';
        array_key_exists($_GET['field'], $search_options) && $field = $_GET['field'];
        $conditions = $this->_get_query_conditions(array(array(
            'field' => $field,       //按用户名,店铺名,支付方式名称进行搜索
            'equal' => 'LIKE',
            'name'  => 'search_name',
        ),array(
            'field' => 'returns.status',
            'name'  => 'status',
            'equal' => '=',
            'type'  => 'numeric',
        ),array(
            'field' => 'returns.add_time',
            'name'  => 'add_time_from',
            'equal' => '>=',
            'handler'=> 'gmstr2time',
        ),array(
            'field' => 'returns.add_time',
            'name'  => 'add_time_to',
            'equal' => '<=',
            'handler'   => 'gmstr2time_end',
        ),array(
            'field' => 'returns.returns_amount',
            'name'  => 'returns_amount_from',
            'equal' => '>=',
            'type'  => 'numeric',
        ),array(
            'field' => 'returns.returns_amount',
            'name'  => 'returns_amount_to',
            'equal' => '<=',
            'type'  => 'numeric',
        ),
        ));

        $model_returns =& m('returns');
        $page   =   $this->_get_page(10);    //获取分页信息
        //更新排序
        if (isset($_GET['sort']) && isset($_GET['order']))
        {
            $sort  = strtolower(trim($_GET['sort']));
            $order = strtolower(trim($_GET['order']));
            if (!in_array($order,array('asc','desc')))
            {
                $sort  = 'returns.add_time';
                $order = 'desc';
            }
        }
        else
        {
            $sort  = 'returns.add_time';
            $order = 'desc';
        }

        $limit = $is_excel ? '' : $page['limit'];
        $filtered = $conditions? 1 : 0;//是否有查询条件

        $returns = $model_returns->find(array(
            'conditions'    => '1=1 ' . $conditions,
            'fields'        => "this.returns_id,this.returns_sn,this.add_time,this.returns_amount,this.status,this.type,this.quantity,this.shipping_fee,this.goods_amount,this.report_reason,this.shipping_status,order_goods.rec_id,order_goods.goods_id,order_goods.goods_image,order_goods.goods_name,order_goods.specification,order_goods.quantity*order_goods.price as order_goods_amount,order_alias.order_sn,order_alias.out_trade_sn,order_alias.seller_name,order_alias.order_amount, order_extm.shipping_fee as order_shipping_fee",
            'limit'         => $limit,  //获取当前页的数据
            'order'         => "$sort $order",
            'join'          => 'belongs_to_order,belongs_to_ordergoods,belongs_to_orderextm',
            'count'         => true           //允许统计
        ));

        if($returns)
        {
            $i = 1;
            foreach($returns as $key => $val)
            {
                $returns[$key]['no'] = $i;
                $returns[$key]['returns_status'] = $cfg_returns['status'][$val['status']][$val['type']]['msg_1'];//退货退款状态提示
                $returns[$key]['type_name'] = $cfg_returns['type'][$val['status']];
                $returns[$key]['add_time'] = date('Y-m-d H:i:s',$returns[$key]['add_time']);
                $i++;
            }

        }

        $page['item_count'] = $model_returns->getCount();   //获取统计的数据
        $this->_format_page($page);
        $arr['page'] = $page;
        $arr['data'] = $returns;
        $arr['filtered'] = $filtered;

        return $arr ;
    }

    function _get_excel_heads()
    {
        return array(
            'no' => array('name' => "序号"),
            'order_sn' => array('name' => "网上订单号"),
            'order_goods_amount' => array('name' => "商品金额"),
            'order_shipping_fee' => array('name' => "运费金额"),
            'order_amount' => array('name' => "订单金额"),
            'returns_sn' => array('name' => "退款号"),
            'type_name' => array('name' => "类型"),
            'goods_amount' => array('name' => "退商品金额"),
            'shipping_fee' => array('name' => "退运费金额"),
            'returns_amount' => array('name' => "退款总金额"),
            'returns_status' => array('name' => "状态"),
            'add_time' => array('name' => "申请时间"),
            'report_reason' => array('name' => "退款原因"),
            'remark' => array('name' => "备注"),

        );
    }


    /**
     * 买家退货退款配置信息
     */
    function _get_cfg_msg()
    {
        //状态 0.无效 1.申请 2.取消  3.卖家同意 4.卖家拒绝  5.买家发货  6.万菱介入 7.确认 8.成功 -1.关闭
        //类型 1.退款  2.退货退款  3.换货
        $cfg_returns = array(
            'type' =>array(
                '1' => '退款',
                '2' => '退货退款',
                '3' => '换货',
            ),
            'starus_type' =>array(
                '0' => '无效',
                '1' => '申请',
                '2' => '取消',
                '3' => '卖家同意',
                '4' => '卖家拒绝',
                '5' => '买家发货',
                '6' => '万菱介入',
                '7' => '确认',
                '8' => '成功',
                '-1' => '关闭',
            ),

            /*1.退款  2.退货退款  3.换货 状态提示信息 */
            'status' => array(
                '0' => array(
                    '1'=>array('msg_1'=>'退款关闭','msg_2'=>'退款关闭。'),
                    '2'=>array('msg_1'=>'退货退款关闭','msg_2'=>'退货退款关闭。'),
                    '3'=>array('msg_1'=>'退款关闭','msg_2'=>'退款关闭。')
                ),
                '1' => array(
                    '1'=>array('msg_1'=>'退款处理中','msg_2'=>'等待卖家处理退款申请。',),
                    '2'=>array('msg_1'=>'退货处理中','msg_2'=>'等待卖家处理退货退款申请。'),
                    '3'=>array('msg_1'=>'退款处理中','msg_2'=>'等待卖家处理退款申请。')
                ),
                '2' => array(
                    '1'=>array('msg_1'=>'退款关闭','msg_2'=>'退款关闭。'),
                    '2'=>array('msg_1'=>'退货退款关闭','msg_2'=>'退货退款关闭。'),
                    '3'=>array('msg_1'=>'退款关闭','msg_2'=>'退款关闭。')
                ),
                '3' => array(
                    '1'=>array('msg_1'=>'','msg_2'=>'','msg_3'=>''),
                    '2'=>array('msg_1'=>'请退货','msg_2'=>'卖家同意退货，等买家发送退货商品。'),
                    '3'=>array('msg_1'=>'请退货','msg_2'=>'卖家同意退货，等买家发送退货商品。')
                ),
                '4' => array(
                    '1'=>array('msg_1'=>'卖家拒绝退款','msg_2'=>'卖家拒绝退款，等买家处理。'),
                    '2'=>array('msg_1'=>'卖家拒绝退货','msg_2'=>'卖家拒绝退货退款，等买家处理。'),
                    '3'=>array('msg_1'=>'请处理','msg_2'=>'卖家拒绝退款，等买家处理。')
                ),
                '5' => array(
                    '1'=>array('msg_1'=>'','msg_2'=>'','msg_3'=>''),
                    '2'=>array('msg_1'=>'等卖家退款','msg_2'=>'等待卖家收货并退款。'),
                    '3'=>array('msg_1'=>'等卖家退款','msg_2'=>'等待卖家收货并退款。')
                ),
                '6' => array(
                    '1'=>array('msg_1'=>'万菱介入中','msg_2'=>'万菱介入中'),
                    '2'=>array('msg_1'=>'万菱介入中','msg_2'=>'万菱介入中'),
                    '3'=>array('msg_1'=>'万菱介入中','msg_2'=>'万菱介入中')
                ),
                '7' => array(
                    '1'=>array('msg_1'=>'退款转账中','msg_2'=>'卖家同意退款。'),
                    '2'=>array('msg_1'=>'退款转账中','msg_2'=>'卖家同意退款。'),
                    '3'=>array('msg_1'=>'退款转账中','msg_2'=>'卖家同意退款。')
                ),
                '8' => array(
                    '1'=>array('msg_1'=>'退款成功','msg_2'=>'退款成功。'),
                    '2'=>array('msg_1'=>'退款成功','msg_2'=>'退款成功。'),
                    '3'=>array('msg_1'=>'退款成功','msg_2'=>'退款成功。')
                ),
                '-1'=> array(
                    '1'=>array('msg_1'=>'退货关闭','msg_2'=>'退货关闭。'),
                    '2'=>array('msg_1'=>'退货关闭','msg_2'=>'退货关闭。'),
                    '3'=>array('msg_1'=>'退货关闭','msg_2'=>'退货关闭。')
                ),
            ),);
        return $cfg_returns;
    }
}
?>
